package others.swing.p011;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

/*学生成绩管理系统 版本T1.0教师操作界面*/
public class TDo extends JFrame implements ActionListener {
    Label t1, t2, t3, t4, sg1, sg2, sg3;
    //JPanel rongqi;
    JTextArea jlstudent;          //用文本框显示学生信息
    JScrollPane gundong;
    JButton b1, b2, b3, b4;
    String ad = "", y = "", q = " ", sql = "";     //ad表示即将要访问的数据库名称（即访问者的身份），y表示访问者的用户名
    DBAccess db = new DBAccess("SMSystem", "", "");//数据库操作
    Teacher t;
    int i = 0, j = 0, p = 0;
    String id[], n[], sc[];    //学号，姓名，班级
    double g1[], g2[], g3[];   //三门课程成绩

    public TDo(String u, String x) {
        ad = u;
        y = x;
        t = new Teacher();
        FindTClass();  //找出该教师所教班级和得到他的ID
        t1 = new Label("欢迎您! " + y);
        t2 = new Label("");
        t3 = new Label("学号");
        t4 = new Label("姓名");
        sg1 = new Label("语文");
        sg2 = new Label("英语");
        sg3 = new Label("数学");
        jlstudent = new JTextArea(6, 10);
        gundong = new JScrollPane(jlstudent);//出现滚动条
        b1 = new JButton("修改学生成绩");
        b2 = new JButton("查看全班成绩");
        b3 = new JButton("修改密码");
        b4 = new JButton("注销");
        setLayout(null);

        //设置标签大小和所在位置
        t1.setBounds(40, 30, 180, 50);
        b1.setBounds(250, 120, 150, 50);
        b2.setBounds(420, 120, 140, 50);
        b3.setBounds(580, 120, 100, 50);
        b4.setBounds(700, 500, 80, 50);

        t2.setBounds(40, 150, 200, 50);
        t3.setBounds(60, 200, 105, 50);
        t4.setBounds(165, 200, 105, 50);
        sg1.setBounds(275, 200, 105, 50);
        sg2.setBounds(385, 200, 105, 50);
        sg3.setBounds(495, 200, 105, 50);
        gundong.setBounds(50, 260, 600, 250);
        //设置标签内的字体
        t1.setFont(new Font("楷体", 1, 20));//字体大小
        t2.setFont(new Font("楷体", 1, 20));//字体大小
        t3.setFont(new Font("楷体", 1, 20));//字体大小
        t4.setFont(new Font("楷体", 1, 20));//字体大小
        jlstudent.setFont(new Font("楷体", 1, 15));//字体大小
        sg1.setFont(new Font("楷体", 1, 20));//字体大小
        sg2.setFont(new Font("楷体", 1, 20));//字体大小
        sg3.setFont(new Font("楷体", 1, 20));//字体大小

        b1.setFont(new Font("楷体", 1, 15));//字体大小
        b2.setFont(new Font("楷体", 1, 15));//字体大小
        b3.setFont(new Font("楷体", 1, 15));//字体大小
        b4.setFont(new Font("楷体", 1, 15));//字体大小

        t1.setForeground(Color.BLUE);//字体颜色
        t2.setForeground(Color.BLUE);//字体颜色
        jlstudent.setForeground(Color.BLUE);//列表框字体颜色设为蓝色
        jlstudent.setCaretPosition(jlstudent.getText().length());//让滚动条自行向下滚动，显示最新输出的内容
        b1.setForeground(Color.BLUE);//字体颜色
        b2.setForeground(Color.BLUE);//字体颜色
        b3.setForeground(Color.BLUE);//字体颜色
        b4.setForeground(Color.BLUE);//字体颜色
        add(t1);
        add(t2);
        add(t3);
        add(t4);
        add(sg1);
        add(sg2);
        add(sg3);
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(gundong);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        //jlstudent.addListSelectionListener(this);
        setSize(800, 600);
        setVisible(true);
    }

    //找出该教师所教班级
    void FindTClass() {
        String tid, tclass;
        int a = 0;        //a=0时是未读取学生信息或者没读取成功
        try {
            db.dbcon();
            sql = "SELECT * FROM teacher";
            db.dbSelect(sql);
            j = 0;   //第二次点击查看学生成绩是时数组返回开头位置开始存储信息
            while (db.rs.next()) {
                j++;
            }
            db.rs.beforeFirst();   //游标返回开头位置
            for (i = 0; i < j; i++) {
                db.rs.next();
                tid = db.rs.getString(2);
                tclass = db.rs.getString(5);
                if (tid.equals(y)) {
                    t.setTID(tid);
                    t.getTID();
                    t.setTClass(tclass);
                    t.getTClass();
                }
            }
            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
    }

    //得到该学生的成绩信息
    int viewGrade() {
        int a = 0, k = 0;        //a=0时是未读取学生信息或者没读取成功
        p = 0;//重新读取数据时从0开始
        try {
            db.dbcon();
            sql = "SELECT * FROM student";
            db.dbSelect(sql);
            j = 0;   //第二次点击查看学生成绩是时数组返回开头位置开始存储信息
            while (db.rs.next()) {
                j++;
            }

            String b[][] = new String[3][j];
            double g[][] = new double[3][j];

            db.rs.beforeFirst();   //游标返回开头位置
            for (i = 0; i < j; i++) {
                db.rs.next();
                b[0][i] = db.rs.getString(2);
                b[1][i] = db.rs.getString(3);
                b[2][i] = db.rs.getString(5);
                g[0][i] = db.rs.getDouble(9);
                g[1][i] = db.rs.getDouble(10);
                g[2][i] = db.rs.getDouble(11);
            }
            for (i = 0; i < j; i++) {
                if (b[2][i].equals(t.TClass)) {
                    p++;
                }//计算出多少个学生是这个老师所教班级的
            }
            id = new String[p];
            n = new String[p];
            sc = new String[p];
            g1 = new double[p];
            g2 = new double[p];
            g3 = new double[p];

            //此处的数组的下标的自增是个难点，这个算法真是不好想啊，容易混
            for (i = 0; i < j; ) {
                if (b[2][i].equals(t.TClass)) {
                    id[k] = b[0][i];
                    n[k] = b[1][i];
                    sc[k] = b[2][i];
                    g1[k] = g[0][i];
                    g2[k] = g[1][i];
                    g3[k] = g[2][i];
                    k++;           //***难点
                    i++;           //***难点
                    a = 1;    //读取成绩成功
                } else {
                    i++;
                }     //***难点
            }

            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    //将学生信息输出
    public String setT() {
        q = "";
        for (i = 0; i < p; i++) {
            q = q + id[i] + "   " + n[i] + "        " + g1[i] + "       " + g2[i] + "        " + g3[i] + "\n";
        }
        return q;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == b1) {
            new CSGrade();
        } else if (e.getSource() == b2) {
            if (viewGrade() == 1) {
                t2.setText(t.TClass + "成绩如下：");
                jlstudent.setText("");
                jlstudent.setText(setT());
            } else {
                JOptionPane.showMessageDialog(null, "读取数据库出错！", "友情提示", 1);
            }
        } else if (e.getSource() == b3) {
            new Cpwd(ad, t.TID);
        } else if (e.getSource() == b4) {
            dispose();//清除界面 setVisible(false);
            new Login();
        }
    }
}